package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.Drug;
import com.ruoyi.project.drug.service.IDrugProducerService;
import com.ruoyi.project.drug.service.IDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @Classname DrugController
 * @Description
 * @Date 2022/6/28 19:56
 * @Author Lee
 */
@RestController
@RequestMapping("/drugs/drug")
public class DrugController extends BaseController {

    @Autowired
    private IDrugService drugService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('drugs:drug:list')")
    public TableDataInfo list(Drug drug) {
        startPage();
        return getDataTable(drugService.selectDrugList(drug));
    }

    @GetMapping("/supportId/{supportId}")
    @PreAuthorize("@ss.hasPermi('drugs:drug:query')")
    public TableDataInfo getInfoBySupportId(@PathVariable Integer supportId) {
        return getDataTable(drugService.selectBySupportId(supportId));
    }

    @GetMapping("/{drugId}")
    @PreAuthorize("@ss.hasPermi('drugs:drug:query')")
    public AjaxResult getInfo(@PathVariable Integer drugId) {
        return AjaxResult.success(drugService.getDrugById(drugId));
    }

    @PostMapping()
    @Log(title = "药品信息维护", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('drugs:drug:add')")
    public AjaxResult add(@Validated @RequestBody Drug drug) {
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugService.insertDrug(drug));
    }

    @PutMapping()
    @Log(title = "药品信息维护", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('drugs:drug:edit')")
    public AjaxResult edit(@Validated @RequestBody Drug drug) {
        drug.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(drugService.updateDrug(drug));
    }


    @DeleteMapping("/{drugIds}")
    @Log(title = "药品信息维护", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('drugs:drug:remove')")
    public AjaxResult batchRemove(@PathVariable Integer[] drugIds) {
        return toAjax(drugService.batchDelete(drugIds));
    }


}
